package com.duyus.ai.controller;

import com.duyus.ai.repository.FileChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;

@RequiredArgsConstructor
@Slf4j
@RestController
@RequestMapping("/ai")
public class CustomerServiceController {

    public final ChatClient serviceChatClient;

//    private final ChatHistoryRepository chatHistoryRepository;

    private final FileChatHistoryRepository fileChatHistoryRepository;

    private final ToolCallback[] allTools;

    @RequestMapping(value = "/service", produces = "text/html;charset=utf-8")
    public Flux<String> service(String prompt, String chatId) {
        // 1.保存会话id
//        chatHistoryRepository.save("service", chatId);
        // 基于文件
        fileChatHistoryRepository.save("service", chatId);
        // 2.请求模型
        return serviceChatClient.prompt()
                .user(prompt)
                .advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
                .tools(allTools)
                .stream()
                .content();
    }


}